package com.talent.study.disruptor.height;

import com.lmax.disruptor.EventTranslator;
import com.lmax.disruptor.dsl.Disruptor;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * @Author: hszhou
 * @Date: 2025/4/19 11:49
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Slf4j
public class TradePublish implements Runnable {

    private final CountDownLatch downLatch;
    private final Disruptor<Trade> disruptor;
    private final static  int PUBLISH_COUNT = 1;
    public TradePublish(CountDownLatch downLatch, Disruptor<Trade> disruptor) {
        this.disruptor = disruptor;
        this.downLatch = downLatch;
    }

    @Override
    public void run() {


        log.info("执行");

        for (int i = 0; i < PUBLISH_COUNT; i++) {
            log.info("发送消息");
            disruptor.publishEvent(new TradeEventTranslator());
        }
        downLatch.countDown();
    }
}

class TradeEventTranslator implements EventTranslator<Trade>{


    private Random random = new Random();
    @Override
    public void translateTo(Trade event, long sequence) {
        this.generateTrade(event);
    }

    private void generateTrade(Trade trade){

        trade.setPrice(random.nextDouble()*9999);
    }
}